/*
 * Route.cpp
 *
 *  Created on: Dec 22, 2014
 *      Author: wasp
 */

#include "Route.hpp"
#include <qtimer.h>
#include "Location.hpp"
#include <qdebug.h>
#include "../security/System.hpp"
#include "../security/Session.hpp"

namespace de
{
namespace heimweghelden
{
namespace location
{

    Route::Route(QObject* parent, double latS, double lonS, double latE, double lonE) : QObject(parent)
    {
        latStart = latS;
        lonStart = lonS;
        latCurrent = latS;
        lonCurrent = lonS;
        latEnd = latE;
        lonEnd = lonE;

        secondsRemaining = -1;
        estimatedDuration = -1;
        homecomingtimer = NULL;
        distanceStraight = Location::distance(latS, lonS, latE, lonE);
        helden = new QList<QString>;
        session = de::heimweghelden::security::System::getInstance()->createSession();
        if ( ! session->statusOk()) {
            //TODO: fehlerbehandlung
        }
    }
    Route::~Route() {
        delete session;
        delete helden;
        if (homecomingtimer != NULL) {
            homecomingtimer->stop();
            delete homecomingtimer;
        }
    }
    void Route::updatePosition(double lat, double lon) {
        latCurrent = lat;
        lonCurrent = lon;
        //TODO: filter and send information to server
        emit onPositionUpdated(lat, lon);
    }
    int Route::updateHelden(QString number, bool active) {
        int index = -1;
        for (int i=0; i<helden->size(); i++) {
            if (helden->at(i) == number) {
                index = i;
                break;
            }
        }
        // noch nicht vorhanden und es soll auf aktiv gesetzt werden: pushen
        if (active && index == -1) {
            helden->append(number);
        } else if(!active && index != -1) {
            helden->removeAt(index);
        }
        return helden->size();
    }
    void Route::updateEstimatedDuration(int seconds) {
        estimatedDuration = seconds;
        secondsRemaining = seconds;
    }
    void Route::checkState() {
        // check time
        // check position
        double dist = Location::distance(latCurrent, lonCurrent, latEnd, lonEnd);
        //
        if (dist < 10 || (secondsRemaining < 10 && dist < 10)) {
            // angekommen
            qDebug() << "angekommen";
            // stop
            if (homecomingtimer != NULL) {
                homecomingtimer->stop();
                delete homecomingtimer;
                homecomingtimer = NULL;
                secondsRemaining = 0;
            }
            emit arrived();
        } else if (secondsRemaining < 10 && dist > 10) {
            // zeit ist abgelaufen und distanz ist zu gross
            // warnlevel entspricht der prozentual zu hinterlegenden strecke

            int warnlevel = (int)(dist / distanceStraight * 100);
            qDebug() << "achtung: " << dist << warnlevel << dist << distanceStraight;
            emit exceeded(warnlevel, secondsRemaining, latCurrent, lonCurrent);
        }
    }
    void Route::decreaseTime() {
        if (secondsRemaining < 2) {
            if (homecomingtimer != NULL) {
                homecomingtimer->stop();
                delete homecomingtimer;
                homecomingtimer = NULL;
                secondsRemaining = 0;
            }
            emit onTimeExpired();
        } else {
            secondsRemaining--;
        }
        checkState();
        emit onTimeLapsed(secondsRemaining);
    }
    void Route::start() {
        homecomingtimer = new QTimer(this);
        connect(
                homecomingtimer,
                SIGNAL(timeout()),
                this,
                SLOT(decreaseTime())
        );
        homecomingtimer->start(1000);
        // erst wenn alle helden informiert
        emit routeStarted();
    }
} /* namespace location */
} /* namespace heimweghelden */
} /* namespace de */
